<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Exception;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Goods extends Api
{

    /**
     * Goods模型对象
     * @var \app\admin\model\Goods
     */
    protected $model = null;
    protected $goodsCommentModel = null;

    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Goods;
        $this->goodsCommentModel = new \app\admin\model\goods\Comment;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 商品列表
     * @return void
     */
    public function index()
    {
        $params = $this->request->param();
        try {
            $where = [];
            $where['shelf'] = 1;
            if (!empty($params['goods_name'])) {
                $where['goods_name'] = ['like', '%' . $params['goods_name'] . '%'];
            }
            if (!empty($params['type'])) {
                $where['type'] = $params['type'];
            }
            if (!empty($params['shop_id'])) {
                $where['shop_id'] = $params['shop_id'];
            }
            if (!empty($params['goods_category_id'])) {
                $where['goods_category_id'] = $params['goods_category_id'];
            }

            $list = $this->model->where($where)->paginate($params['page_size']);
            $list->each(function ($item) {
                // 检查images字段是否存在且不为空
                if (!empty($item['images'])) {
                    // 按逗号分割字符串为数组
                    $item['images_arr'] = explode(',', $item['images']);
                } else {
                    // 为空时赋空数组（可选）
                    $item['images_arr'] = [];
                }
                return $item;
            });


        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('查询成功', $list);
    }

    /**
     * 新增商品
     * @return void
     */
    public function add()
    {
        $params = $this->request->param();
        try {

            //是否采用模型验证
            $validate_class = '\app\admin\validate\Goods';
            $validate = validate($validate_class);
            if (!$validate->check($params, [], "add")) {
                throw new Exception($validate->getError());
            }

            $params['shelf'] = 1;
            $params['shop_id'] = 1;
            $params['user_id'] = $this->auth->id;
            $this->model->allowField(true)->save($params);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('新增成功', $params);
    }

    /**
     * 商品详情
     * @return void
     */
    public function info()
    {
        $params = $this->request->param();
        try {
            if (empty($params['id'])) {
                throw new Exception('id不能为空');
            }
            $data = $this->model->find($params['id']);


            $data['comment']= $this->goodsCommentModel->where('goods_id', '=', $params['id'])->select();


            if (!empty($data['images'])) {
                // 按逗号分割字符串为数组
                $data['images_arr'] = explode(',', $data['images']);
            } else {
                // 为空时赋空数组（可选）
                $data['images_arr'] = [];
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('查询成功', $data);
    }

    /**
     * 编辑商品
     * @return void
     */
    public function edit()
    {

    }

    /**
     * 删除商品
     * @return void
     */
    public function delete()
    {
        $params = $this->request->param();
        try {
            if (empty($params['id'])) {
                throw new Exception('id不能为空');
            }
            $data = $this->model->destroy($params['id']);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功', $data);
    }
}
